package com.dayatang.android.Adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.dayatang.android.Activity.R;
import com.dayatang.android.Activity.WorkLoadReportActivity;
import com.dayatang.android.Activity.WorkstationOfPMOActivity;
import com.dayatang.android.config.Config;

public class PMSubProjectStatusListViewAdapter extends BaseAdapter {
	
	private WorkstationOfPMOActivity context;
	private List<HashMap<String, Object>> listItem;
	private LayoutInflater listContainer;           

	class ListItemView{
		private TextView subProjNameTextView;
		private TextView currentWorkStepNameTextView;
		private TextView outputTextView;
		private Button reportWorkLoadButton;
	}
	
	public PMSubProjectStatusListViewAdapter(Context context, List<HashMap<String, Object>> listItem) {
		this.listItem = listItem;
		this.context = (WorkstationOfPMOActivity) context;
		listContainer = LayoutInflater.from(context);  
	}

	public int getCount() {
		return listItem.size();
	}

	public Object getItem(int position) {
		return listItem.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		ListItemView  listItemView = null;
		
		if(convertView == null) {
			listItemView = new ListItemView();
			convertView = listContainer.inflate(R.layout.pm_subprojectstatus_listview_item, null);
			
			listItemView.subProjNameTextView = (TextView) convertView.findViewById(R.id.pm_subproject_current_name);
			listItemView.currentWorkStepNameTextView = (TextView) convertView.findViewById(R.id.pm_current_workstep_name);
			listItemView.outputTextView = (TextView) convertView.findViewById(R.id.pm_subproject_output);
			listItemView.reportWorkLoadButton = (Button) convertView.findViewById(R.id.pm_reportworkload_button);
			
            convertView.setTag(listItemView);    
		} else {    
            listItemView = (ListItemView)convertView.getTag();    
        }  
		
		listItemView.subProjNameTextView.setText((String)listItem.get(position).get("ItemSubProjectName"));
		listItemView.currentWorkStepNameTextView.setText((String)listItem.get(position).get("ItemCurrentWorkStep"));
		listItemView.outputTextView.setText((String)listItem.get(position).get("ItemSubProjectOutput"));
		final String subprojectid = (String)listItem.get(position).get("ItemSubProjectId");
		
		listItemView.reportWorkLoadButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(context,WorkLoadReportActivity.class);
				intent.putExtra(Config.SUBPROJID, subprojectid);
				context.startActivity(intent);
			}
		});
		
		return convertView;
	}

}
